#ifndef NDK_PLAYER_CODE_BASECHANNEL_H
#define NDK_PLAYER_CODE_BASECHANNEL_H

#include "safe_queue.h"

extern "C" {
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <libavutil/time.h>
};

class BaseChannel {

public:
    int stream_index;
    AVCodecContext *avCodecContext;

    BaseChannel(int stream_index, AVCodecContext *avCodecContext) {
        this->stream_index = stream_index;
        this->avCodecContext = avCodecContext;
        packets.setReleaseCallback(releaseAVPacket);
        frames.setReleaseCallback(releaseAVFrame);
    }


    // 注意：由于是父类，析构函数，必须是虚函数
    virtual ~BaseChannel() {
        packets.clearQueue();
        frames.clearQueue();
    }

    /**
        * 释放AVPacket 队列
        * @param avPacket
        */
    static void releaseAVPacket(AVPacket **avPacket) {
        if (avPacket) {
            av_packet_free(avPacket);
            *avPacket = 0;
        }
    }

    /**
     * 释放AVFrame 队列
     * @param avFrame
     */
    static void releaseAVFrame(AVFrame **avFrame) {
        if (avFrame) {
            av_frame_free(avFrame);
            *avFrame = 0;
        }
    }

    // 未解码   音频：aac，  视频：h264
    SafeQueue<AVPacket *> packets;

    // 解码后  音频：pcm    视频：yuv
    SafeQueue<AVFrame *> frames;

    bool isPlaying = 1;
};


#endif